<?php

/* @var $this yii\web\View */
/* @var $generator backend\modules\gii\generators\model\Generator */
/* @var $tableName string full table name */
/* @var $className string class name */
/* @var $tableSchema yii\db\TableSchema */
/* @var $labels string[] list of attribute labels (name => label) */
/* @var $rules string[] list of validation rules */
/* @var $relations array list of relations (name => relation declaration) */

echo "<?php\n";
?>

namespace <?= $generator->ns ?>;

use Yii;

/**
 * This is the model class for table "<?= $generator->generateTableName($tableName) ?>".
 *
<?php foreach ($tableSchema->columns as $column): ?>
 * @property <?= "{$column->phpType} \${$column->name}\n" ?>
<?php endforeach; ?>
<?php if (!empty($relations)): ?>
<?php foreach ($relations as $name => $relation): ?>
 * @property <?= $relation[1] . ($relation[2] ? '[]' : '') . ' $' . lcfirst($name) . "\n" ?>
<?php endforeach; ?>
<?php endif; ?>
 */
class <?= $className ?> extends <?= '\\' . ltrim($generator->baseClass, '\\') . "\n" ?>
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '<?= $generator->generateTableName($tableName) ?>';
    }
<?php if ($generator->db !== 'db'): ?>

    /**
     * @return \yii\db\Connection the database connection used by this AR class.
     */
    public static function getDb()
    {
        return Yii::$app->get('<?= $generator->db ?>');
    }
<?php endif; ?>

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [<?= "\n            " . implode(",\n            ", $rules) . "\n        " ?>];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
<?php foreach ($labels as $name => $label): ?>
            <?= "'$name' => " . $generator->generateString($label) . ",\n" ?>
<?php endforeach; ?>
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        $behaviors = parent::behaviors();
<?php
// 创建时间和更新时间
if (isset($labels['created_at']) && isset($labels['updated_at']))
{
    ?>

        $behaviors[] = [
            'class' => \common\behaviors\TimestampBehavior::className(),
            'createdAtAttribute' => 'created_at',
            'updatedAtAttribute' => 'updated_at',
            'value' => new \yii\db\Expression('NOW()'),
        ];
    <?php
}
?>
<?php
// 创建用户和更新用户
if (isset($labels['created_by']) && isset($labels['updated_by']))
{
    ?>
    <?php
}
?>

        return $behaviors;
    }

<?php foreach ($relations as $name => $relation): ?>

    /**
     * @return \yii\db\ActiveQuery
     */
    public function get<?= $name ?>()
    {
        <?= $relation[0] . "\n" ?>
    }
<?php endforeach; ?>

    <?php
    // 一些特殊字段的额外处理
    foreach ($tableSchema->columns as $column)
    {
        switch ($column->name)
        {
            case 'status':
                ?>
    /**
     * <?php echo $column->name ?>对应的工作流列表
     *
     * @return array
     */
    public function <?php echo $column->name ?>Workflow()
    {
        // 简单的流程定义
        return [
            'initStatus' => 'draft',
            'startStatus' => '开始',
            'endStatus' => '结束',
            'status' => [
                'draft' => [
                    'label' => '草稿',
                    'next' => 'audit',
                ],
                'audit' => [
                    'label' => '审核中',
                    'direction' => 'right',
                    'next' => [
                        'true' => ['bottom', 'publish'],
                        'false' => 'draft',
                    ],
                ],
                'publish' => [
                    'label' => '发布中',
                    'direction' => 'right',
                    'next' => [
                        'true' => 'publish_success',
                        'false' => 'publish_failed',
                    ],
                ],
                'publish_failed' => [
                    'label' => '发布失败',
                    'direction' => 'left',
                    'next' => 'draft',
                ],
                'publish_success' => [
                    'label' => '发布成功',
                    //'direction' => 'right',
                    'next' => 'archived',
                ],
                'archived' => [
                    'label' => '存档',
                    'direction' => 'right',
                    'next' => '_end',
                ],
            ],
        ];
    }
                <?php
                break;
            default:
                //
        }
    }
    ?>

}
